import { OrderQueryType, OrderType } from "src/config/defaultconfig";
import { ObjectLiteral, Repository, SelectQueryBuilder } from "typeorm";
import {isNil} from 'lodash'
import { getOrderByQuery } from "src/config/helper";

export abstract class BaseRepository<E extends ObjectLiteral> extends Repository<E>{

    protected abstract _qbName: string

    protected orderBy?: string | { name: string; order: `${OrderType}` }

    //返回查询器名称
    get qbName() {
        return this._qbName
    }

    //返回基础查询器
    buildBaseQB(): SelectQueryBuilder<E> {
        return this.createQueryBuilder(this.qbName)
    }

    //生成基础排序结构
    addOrderByQuery(qb:SelectQueryBuilder<E>,orderBy:OrderQueryType):SelectQueryBuilder<E>{
          let orderByType=orderBy ?? this.orderBy

          return !isNil(orderByType) ? getOrderByQuery(qb, this.qbName, orderByType) : qb;
    }

}